<template>
  <div class="sa-goods sa-flex sa-col-top" :class="`goods-${mode}`">
    <sa-image :url="goods.image" :size="64" radius="8"></sa-image>
    <div class="sa-m-l-12 sa-flex-1">
      <!-- 商品列表 -->
      <template v-if="mode == 'goods'">
        <div class="title sa-table-line-1" @click="openGoods(goods.id)">
          {{ goods.title }}
        </div>
        <div class="subtitle sa-table-line-1 sa-m-b-6">
          {{ goods.subtitle }}
        </div>
        <div class="sa-flex sa-flex-wrap">
          <div v-if="goods.is_sku" class="sku">多规格</div>
          <template v-for="(value, key) in tempActivities" :key="key">
            <el-popover popper-class="sa-popper" trigger="hover">
              <div>
                <div
                  class="activity-title"
                  v-for="item in value"
                  :key="item"
                  @click="openDetail(item)"
                >
                  {{ item.title }}
                </div>
              </div>
              <template #reference>
                <div>
                  <div v-if="value.length > 0" :class="['goods-tag', 'sa-line-1', `${key}-goods`]">
                    {{ value[0].type_text }}
                  </div>
                </div>
              </template>
            </el-popover>
          </template>
          <template v-for="pr in goods.promos" :key="pr">
            <el-popover popper-class="sa-popper" trigger="hover">
              <div class="activity-title" @click="openDetail(pr)">{{ pr.title }}</div>
              <template #reference>
                <div class="goods-tag sa-line-1 promos-goods">
                  {{ pr.type_text }}
                </div>
              </template>
            </el-popover>
          </template>
        </div>
      </template>

      <!-- 积分商品 -->
      <template v-if="mode == 'scoreShop'">
        <div
          class="title sa-table-line-1"
          :class="isGoods ? 'is-active' : ''"
          @click="openGoods(goods.id)"
        >
          {{ goods.title }}
        </div>
        <div class="subtitle sa-table-line-1 sa-m-b-6">
          {{ goods.subtitle }}
        </div>
        <slot name="sku">
          <div class="sku" v-if="goods.sku_text">{{ goods.sku_text }}</div>
        </slot>
      </template>

      <!-- 订单列表 -->
      <template v-if="mode == 'order'">
        <div
          class="title sa-m-b-4 sa-table-line-1"
          :class="isGoods ? 'is-active' : ''"
          @click="openGoods(goods.id)"
        >
          {{ goods.title }}
        </div>
        <div class="sa-flex sa-row-between sa-m-b-2">
          <div v-if="goods.sku_text" class="sa-flex">
            <div class="label">规格：</div>
            <div class="content">{{ goods.sku_text }}</div>
          </div>
          <div class="sa-flex">
            <div class="label">单价：</div>
            <div class="content sa-flex">
              <div>{{ goods.price }}</div>
              <div class="sa-m-l-8">x{{ goods.num }}</div>
            </div>
          </div>
        </div>
        <div class="sa-flex">
          <slot name="tag"></slot>
        </div>
      </template>

      <!-- 售后列表 -->
      <template v-if="mode == 'aftersale'">
        <div
          class="title sa-table-line-1 sa-m-b-6"
          :class="isGoods ? 'is-active' : ''"
          @click="openGoods(goods.id)"
        >
          {{ goods.title }}
        </div>
        <div class="sa-flex sa-m-b-12">
          <div class="price" v-if="goods.price">¥{{ goods.price }}</div>
          <div class="num sa-m-l-8" v-if="goods.num">x{{ goods.num }}</div>
          <div class="expand sa-m-l-4">
            <slot name="expandIcon"></slot>
          </div>
        </div>
        <slot name="sku">
          <div class="sku" v-if="goods.sku_text">{{ goods.sku_text }}</div>
        </slot>
      </template>
      <!-- 售后详情 -->
      <template v-if="mode == 'aftersale-detail'">
        <div
          class="title sa-table-line-1 sa-m-b-6"
          :class="isGoods ? 'is-active' : ''"
          @click="openGoods(goods.id)"
        >
          {{ goods.title }}
        </div>
        <div class="sa-flex sa-m-b-16">
          <div class="label">单价：</div>
          <div class="content sa-flex">
            <div class="price">{{ goods.price }}</div>
            <div class="num sa-m-l-4">x{{ goods.num }}</div>
          </div>
        </div>
        <div class="sa-flex" v-if="goods.sku_text">
          <div class="label">规格：</div>
          <div class="content">{{ goods.sku_text }}</div>
        </div>
      </template>

      <!-- 订单详情/库存补充记录/库存预警 -->
      <template v-if="mode == 'order-detail' || mode == 'stockLog' || mode == 'stockWarning'">
        <div class="sa-flex sa-m-b-8">
          <div
            class="title sa-table-line-1"
            :class="isGoods ? 'is-active' : ''"
            @click="openGoods(goods.id)"
          >
            {{ goods.title }}
          </div>
        </div>
        <div v-if="goods.sku_text" class="sku sa-table-line-1">{{ goods.sku_text }}</div>
      </template>
    </div>
  </div>
</template>
<script>
  export default {
    name: 'GoodsItem',
  };
</script>
<script setup>
  import { computed } from 'vue';
  import { useModal } from '@/sheep/hooks';
  import GoodsEdit from '@/app/shop/admin/goods/goods/edit.vue';
  import ActivityEdit from '@/app/shop/admin/activity/activity/edit.vue';

  const props = defineProps({
    goods: {
      type: Object,
    },
    mode: {
      type: String,
    },
    isGoods: {
      type: Boolean,
      default: false,
    },
  });

  const tempActivities = computed(() => {
    let obj = {
      groupon: [],
      groupon_ladder: [],
      seckill: [],
    };

    if (props.goods.activities && props.goods.activities.length > 0) {
      props.goods.activities.forEach((item) => {
        if (item.type == 'groupon') {
          obj.groupon.push(item);
        }
        if (item.type == 'groupon_ladder') {
          obj.groupon_ladder.push(item);
        }
        if (item.type == 'seckill') {
          obj.seckill.push(item);
        }
      });
    }

    return obj;
  });

  function openGoods() {
    if (props.isGoods) {
      useModal(GoodsEdit, {
        title: '商品',
        type: 'edit',
        id: props.goods.id,
      });
    }
  }

  function openDetail(data) {
    useModal(ActivityEdit, {
      title: `编辑${data.type_text}`,
      type: 'edit',
      atype: data.type,
      id: data.id,
    });
  }
</script>
<style lang="scss" scoped>
  .sa-goods {
    .title {
      height: 14px;
      line-height: 14px;
      font-size: 12px;
      font-weight: 600;
      color: var(--sa-subtitle);
      &.is-active {
        color: var(--el-color-primary);
        cursor: pointer;
      }
    }
    .price,
    .num {
      line-height: 14px;
      font-size: 12px;
      color: var(--sa-font);
      flex-shrink: 0;
    }
    .expand {
      font-size: 12px;
      color: var(--sa-font);
    }
    .sku {
      width: fit-content;
      height: 18px;
      line-height: 18px;
      padding: 0 8px;
      font-size: 12px;
      color: var(--sa-subfont);
      background: var(--sa-space);
      border-radius: 10px;
    }
    .label {
      line-height: 14px;
      font-size: 12px;
      color: var(--sa-subfont);
    }
    .content {
      line-height: 14px;
      font-size: 12px;
      color: var(--sa-subtitle);
    }

    .goods-tag {
      padding: 3px 8px;
      border-radius: 10px;
      line-height: 14px;
      font-size: 12px;
      font-weight: 400;
      margin-right: 8px;
      cursor: pointer;
      &.promos-goods {
        color: #faad14;
        background: rgba(250, 173, 20, 0.16);
      }
      &.groupon_ladder-goods,
      &.groupon-goods {
        color: var(--el-color-primary);
        background: var(--t-bg-active);
      }
      &.seckill-goods {
        color: #ff4d4f;
        background: rgba(255, 77, 79, 0.16);
      }
    }

    &.goods-goods {
      .title {
        height: 20px;
        line-height: 20px;
        font-size: 14px;
        font-weight: 500;
        color: var(--sa-font);
      }
      .subtitle {
        height: 16px;
        line-height: 16px;
        font-size: 12px;
        font-weight: 400;
        color: var(--sa-subfont);
      }
      .sku {
        background: var(--el-color-primary);
        color: var(--sa-background-assist);
      }
    }

    &.goods-scoreShop {
      .title {
        height: 20px;
        line-height: 20px;
        font-size: 14px;
        font-weight: 500;
        color: var(--sa-font);
      }
      .subtitle {
        height: 16px;
        line-height: 16px;
        font-size: 12px;
        font-weight: 400;
        color: var(--sa-subfont);
      }
    }

    &.goods-order {
      .sa-image {
        width: 58px !important;
        height: 58px !important;
      }
      .title {
        height: 16px;
        line-height: 16px;
        font-size: 12px;
        font-weight: 500;
      }
      .label,
      .content {
        line-height: 16px;
        font-size: 12px;
        font-weight: 400;
        color: var(--sa-font);
      }
    }

    &.goods-order-detail,
    &.goods-stockLog,
    &.goods-stockWarning {
      .sa-image {
        width: 48px !important;
        height: 48px !important;
      }
      .title {
        height: 20px;
        line-height: 20px;
        font-size: 14px;
        font-weight: 500;
        color: var(--sa-font);
      }
    }
  }
  .activity-title {
    &:hover {
      color: var(--el-color-primary);
      cursor: pointer;
    }
  }
</style>
